#ifdef _WIN32

#include <Windows.h>
#include <cstdint>
#include <conio.h>  // _kbhit
#include <cstdio>
#include <ctime>
#include <adt.h>

HANDLE hStdin = INVALID_HANDLE_VALUE;

/* Input Buffering Windows */
DWORD fdwMode, fdwOldMode;

/* Check Key Windows */
UINT16 checkKey()
{
    return WaitForSingleObject(hStdin, 1000) == WAIT_OBJECT_0 && _kbhit();
}

void disableInputBuffer()
{
    // 2021-05-31 Get standard input device. Initially is console input buffer
    hStdin = GetStdHandle(STD_INPUT_HANDLE);
    GetConsoleMode(hStdin, &fdwOldMode); /* save old mode */
    fdwMode = fdwOldMode
              ^ ENABLE_ECHO_INPUT  /* no input echo */
              ^ ENABLE_LINE_INPUT; /* return when one or more characters are 
                                      available, not must carriage return */
    SetConsoleMode(hStdin, fdwMode); /* set new mode */
    FlushConsoleInputBuffer(hStdin); /* clear buffer */
}

void restoreInputBuffer()
{
    SetConsoleMode(hStdin, fdwOldMode);
}

/**
 * Handle Interrupt
 * 2021-05-31 this callback func will be invoked by terminating program
 * by ctrl + c
 **/
void handleInterrupt(int signal)
{
    restoreInputBuffer();
    fprintf(stderr, "Interrupt caught.\n");
    exit(-2);
}

#endif
